package cn.project.service.imple;

import cn.project.dao.IUserMapper;
import cn.project.model.Page;
import cn.project.model.User;
import cn.project.service.IUserService;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.Map;

@Service
public class UserServiceImple implements IUserService {
    @Resource
    private IUserMapper userMapper;

    @Override
    public Page queryLikeUsers(Map<String, Object> cond) {

               Page page=new Page();
        /*按条件查询总数赋值给Page的totalNum*/
        page.setTotalNum(userMapper.getLikeUsersCount(cond));
        /*请求参数中获取每页大小*/
        int pageSize=Integer.parseInt(String.valueOf(cond.get("pageSize")));
        page.setPageSize(pageSize);
        int currPageNum=Integer.parseInt(String.valueOf(cond.get("pageNum")));
        page.setCurPageNum(currPageNum);
        // 动态计算总页数(总记录数 除以 每页大小，加上 总页数 求余 每页大小，如果余数不为0，则 加 1，否则 加 0 )
        page.setTotalPage(page.getTotalNum() / pageSize + (page.getTotalNum() % pageSize == 0 ? 0 : 1));
        // 根据条件查询符合的用户列表记录，赋值给page的result变量
        page.setResult(userMapper.getLikeUsers(cond));
        return page;
    }

    @Override
    public void addUser(User user) {
          userMapper.addUser(user);
    }

    @Override
    public void delUser(User user) {
          userMapper.delUser(user);
    }

    @Override
    public User getUser(User user) {
        return userMapper.getUser(user);
    }

    @Override
    public void editUser(User user) {
        userMapper.editUser(user);
    }

    public IUserMapper getUserMapper() {
        return userMapper;
    }

    public void setUserMapper(IUserMapper userMapper) {
        this.userMapper = userMapper;
    }
}
